<?php
namespace app\pc;
use think\Cache;
use think\Model;
use think\Request;
use app\pc\model\Article as ArticleModel;


class BaseModel extends Model
{
    public $request;
    public $requestData;
    public $articleNew;
    protected $user;
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->request = Request::instance();
        $this->requestData = empty($data)?$this->request->param(''):$data;
        if($this->request->__get('user')){
            $this->user = $this->request->__get('user');
        }
        if(isset($this->ParameterDetection)){
            call_user_func([$this, 'checkData']);
        }
        $this->articleNew = new ArticleModel;

        //跨域问题
        header("Access-Control-Allow-Origin:*");
        header('Access-Control-Allow-Method:POST,GET');//允许访问的方式 
        header('Access-Control-Allow-Headers:x-requested-with, content-type');
    }

    /**
     * 路由检测以及参数是否已传
     */
    public function checkData()
    {
        $path_info = $_SERVER['REQUEST_URI'];
        $path_info = substr($path_info,1);
        $path = explode('/', $path_info);
        $path_count = count($path);

        if(count($path) > 3)die(json_encode(['data'=>[],'code'=>'-1','message'=>'地址不合法']));
        if(isset($this->ParameterDetection[$path[2]])){
            foreach($this->ParameterDetection[$path[2]] as $k => $v){
                if(!array_key_exists($v,$this->requestData))die(json_encode(['data'=>[],'code'=>'-1','message'=>$v.'参数未传']));
            }
        }
    }


}